#!/usr/bin/perl
use strict;
package parse;
#use modules::encode;

sub burst {
#1  2 3          4         5      6   7  8                                         9
#AZ B #coder-com 949217470 +tinkl key 56 AAAAA,AAAAB,AAAAC,ABAAA,ABAAB,ABAAC,ACAAA :%*!*@*.net
    my $dbh = shift;
    my $sth;
    my $input = shift;
    my $csnum = shift;
    my $cdebug = shift;
    my $remote = shift;
    my $burstusers;
    my $bans;
    my $amion;
    $sth = $dbh->prepare("select channel from channels where channel = '$$input[2]';");
    $sth->execute;
    $amion = $sth->fetchrow_array;
    if ($amion) {
    $sth = $dbh->prepare("update channels set bursttime = '$$input[3]' where channel = '$$input[2]';");
    $sth->execute;

    }
    if ($$input[4] !~ /\+/) {
	$burstusers = $$input[4];
	$bans = 5

       
    }
    elsif ($$input[4] =~ /k/ && $$input[4] =~ /l/) {
	$burstusers = $$input[7];
	$bans = 8;
	
    }
    elsif ($$input[4] =~ /k/ || $$input[4] =~ /l/) {
	$burstusers = $$input[6];
	$bans = 7;
	
    }
    else {
	$burstusers = $$input[5];
	$bans = $$input[6];
    
    }
    my @burstusers = split(/,/,$burstusers);
    foreach my $i (@burstusers) {
	my @tmp = split(/:/,$i);
	my $nick;
	my $encode = encode($tmp[0]);
	
	if ($tmp[1] eq 'o') {
	    $sth=$dbh->prepare("INSERT INTO onchan VALUES ('$tmp[0]','$$input[2]','1','0','$encode');");
	    $sth->execute;
	}
	if ($tmp[1] eq 'v') {
            $sth=$dbh->prepare("INSERT INTO onchan VALUES ('$tmp[0]','$$input[2]','0','1','$encode');");
            $sth->execute;
	}
	if ($tmp[1] eq 'ov') {
            $sth=$dbh->prepare("INSERT INTO onchan VALUES ('$tmp[0]','$$input[2]','1','1','$encode');");
            $sth->execute;
	}
	if ($tmp[1] eq "") {
	    $sth=$dbh->prepare("INSERT INTO onchan VALUES ('$tmp[0]','$$input[2]',0,0,'$encode');");
	    $sth->execute;
	}
    }
}

sub endburst {
    my $dbh = shift;
    my $remote = shift;
    my $csnum = shift;
    my $cdebug = shift;
    my $num = shift;
    my $sth;
    $sth = $dbh->prepare("SELECT channel,bursttime,deftopic,stricttopic FROM channels;");
    $sth->execute;
    while (my @bursting = $sth->fetchrow_array) {
	print $remote "$num B $bursting[0] $bursting[1] +nt $csnum".":o\n";
	print $remote "$csnum T $bursting[0] :$bursting[2]\n" if ($bursting[3] eq '1');
    }
    print $remote "$num EB\n";
}


sub join {
    my ($dbh,$tmpnum,$chan,$remote,$csnum) = @_;
    my $encode = encode($tmpnum);
    my @chans = split(/,/,$chan);
    my $sth=$dbh->prepare("SELECT auth FROM ninfo WHERE num = '$tmpnum' AND code = '$encode';");
    $sth->execute;
    my $rnick = $sth->fetchrow_array;
    foreach $chan (@chans) {
  
	$sth=$dbh->prepare("SELECT rnick FROM users WHERE channel = '$chan' AND level > 99 and rnick = '$rnick' and aop = '1';");
	$sth->execute;
	my $true = $sth->fetchrow_array;
	$sth=$dbh->prepare("SELECT rnick FROM users WHERE channel = '$chan' AND rnick = '$rnick' and avoice = '1';");
	$sth->execute;
	my $true2 = $sth->fetchrow_array;
	if ($true) {
	    print $remote "$csnum M $chan +o $tmpnum\n";
	    $sth = $dbh->prepare("INSERT INTO onchan VALUES ('$tmpnum', '$chan',1,0,'$encode');");
	    $sth->execute;
	}
	elsif ($true2) {
	    print $remote "$csnum M $chan +v $tmpnum\n";
	    $sth=$dbh->prepare("INSERT INTO onchan VALUES ('$tmpnum','$chan',0,1,'$encode');");
	    $sth->execute;
	}
	else {
	    $sth = $dbh->prepare("INSERT INTO onchan VALUES ('$tmpnum', '$chan',0,0,'$encode');");
	    $sth->execute;
	}
    }


    undef @chans,$chan,$encode;
}
sub create {
    my ($dbh,$tmpnum,$chan) = @_;
    my $sth;
    my $encode = encode($tmpnum);

    $sth = $dbh->prepare("INSERT INTO onchan VALUES ('$tmpnum','$chan',1,0,'$encode');");
    $sth->execute;
}

sub part {
    my $dbh = shift;
    my $tmpnum = shift;
    my $chan = shift;
    my $encode = encode($tmpnum);
    my $sth=$dbh->prepare("DELETE FROM onchan WHERE num = '$tmpnum' AND channel = '$chan' AND code = '$encode';");
    $sth->execute;
}

sub kick {
    my ($dbh,$input) = @_;
    #AFAA5 K #hi AFAA5 :tonberry
    my $encode = encode($$input[3]);
    my $sth = $dbh->prepare("DELETE FROM onchan WHERE num = '$$input[3]' AND channel = '$$input[2]' AND code = '$encode';");
    $sth->execute;
}


sub quit {
    my $dbh = shift;
    my $tmpnum = shift;
    my $chan = shift;
    my $sth;
    $sth=$dbh->prepare("DELETE FROM onchan WHERE num = '$tmpnum' and code = '".encode($tmpnum)."';");
    $sth->execute;
    $sth=$dbh->prepare("DELETE FROM ninfo WHERE num = '$tmpnum' AND code = '".encode($tmpnum)."';");
    $sth->execute;
}
sub encode {
    my $code = $_[0];
    my @code = split(//,$code);
    my $return;
    foreach (@code) {
        if ($_ eq lc($_)) {
            $return .= '1';
        }
        else {
            $return .= '2';
        }
    }
    return $return;
}                

1;
